package org.herrdommel.wakemeup.app;

import org.herrdommel.wakemeup.R;
import org.herrdommel.wakemeup.util.DialogFactory;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.DigitalClock;

public class AlarmListActivity extends FragmentActivity {

	// UI elements
	private AnalogClock aClockMainView;
	private DigitalClock dClockMainView;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_list);
		aClockMainView = (AnalogClock) findViewById(R.id.aClockMainView);
		dClockMainView = (DigitalClock) findViewById(R.id.dClockMainView);
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshClockStyle();
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_alarm_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
			case R.id.mnuNewAlarm :
				final Intent newAlarmIntent = new Intent(this, AlarmEditor.class);
				startActivity(newAlarmIntent);
				return true;
			case R.id.mnuAbout :
				showDialog(DialogFactory.DIALOG_ABOUT);
				return true;
			case R.id.mnuSettings :
				final Intent settingsIntent = new Intent(this, ApplicationSettings.class);
				startActivity(settingsIntent);
				return true;
			default :
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected Dialog onCreateDialog(final int id) {
		final Dialog dialog = DialogFactory.createDialog(id, this);
		if (dialog != null) {
			return dialog;
		} else {
			return super.onCreateDialog(id);
		}
	}

	/** Refreshes the clock style according to the preferences */
	private void refreshClockStyle() {
		final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		final boolean clockStyle = sPrefs.getBoolean(getString(R.string.prefKeyClockStyle), false);
		if (clockStyle) {
			dClockMainView.setVisibility(View.VISIBLE);
			aClockMainView.setVisibility(View.GONE);
		} else {
			dClockMainView.setVisibility(View.GONE);
			aClockMainView.setVisibility(View.VISIBLE);
		}
	}

}
